﻿using Microsoft.Xna.Framework;
using ShevaEngine.Core.Interfaces;
using ShevaEngine.Core.Modules.Graphics;
using ShevaEngine.Core.Modules.Graphics.Cameras;

namespace ShevaEngine.Core.Modules.Scenes
{
    /// <summary>
    /// Draw call node.
    /// </summary>
    public class SceneNodeDrawCall : SceneNode
    {
        /// <summary>DrawCallKey.</summary>
        public DrawCallKey DrawCallKey;
        /// <summary>Draw call.</summary>
        public DrawCall DrawCall;

        /// <summary>
        /// Constructor.
        /// </summary>
        public SceneNodeDrawCall()
            : base()
        {

        }

        /// <summary>
        /// Override Add in rendering pipeline method.
        /// </summary>
        /// <param name="renderingPipeline"></param>
        /// <param name="worldMatrix"></param>
        public override bool AddInRenderingPipeline(RenderingPipeline renderingPipeline, Camera camera, Matrix worldMatrix)
        {
            if (this.BoundingSphere == null || this.BoundingSphere.Transform(worldMatrix).Intersects(camera.Frustum))
                renderingPipeline.AddOpaqueGraphicsObject(this.DrawCallKey, this.DrawCall, worldMatrix);

            return false;
        }

        /// <summary>
        /// Override Add in rendering pipeline method.
        /// </summary>
        /// <param name="renderingPipeline"></param>
        /// <param name="worldMatrix"></param>
        public override bool AddInRenderingPipeline(RenderingPipeline renderingPipeline, Vector3 position, float distanceSquared, Matrix worldMatrix)
        {
            if (Vector3.DistanceSquared(this.BoundingSphere.Transform(worldMatrix).Center, position) -
                (this.BoundingSphere.Radius * this.BoundingSphere.Radius) < distanceSquared)
                renderingPipeline.AddOpaqueGraphicsObject(this.DrawCallKey, this.DrawCall, worldMatrix);

            return false;
        }

        /// <summary>
        /// Method setups draw call key.
        /// </summary>
        /// <param name="drawCallKey"></param>
        public override void SetupDrawCallKey(DrawCallKey drawCallKey)
        {
            this.DrawCallKey.MaterialID = drawCallKey.MaterialID;
            this.DrawCallKey.TextureState = drawCallKey.TextureState;
            this.DrawCallKey.MaterialParamsID = drawCallKey.MaterialParamsID;            

            base.SetupDrawCallKey(drawCallKey);
        }
    }
}
